package cn.edu.ujn.liuklab3.service;

import cn.edu.ujn.liuklab3.entity.Table;
import cn.edu.ujn.liuklab3.dao.TableDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("tableService")
public class TableServiceImpl implements TableService {
    @Resource
    private TableDao tableDao;

    @Override
    public Table queryById(Integer tableId ) {
        return this.tableDao.queryById(tableId);
    }

    @Override
    public Page<Table> queryByPage(Table table, PageRequest pageRequest) {
        long total = this.tableDao.count(table);
        return new PageImpl<>(this.tableDao.queryAllByLimit(table, pageRequest), pageRequest, total);
    }

    @Override
    public Table insert(Table table) {
        this.tableDao.insert(table);
        return table;
    }

    @Override
    public boolean deleteById(Integer tableId) {
        return this.tableDao.deleteById(tableId) > 0;
    }

    @Override
    public cn.edu.ujn.util.Page<Table> selectAll(Table table) {
        List<Table> tables = tableDao.selectAll(table);
        cn.edu.ujn.util.Page<Table> page = new cn.edu.ujn.util.Page<>();
        page.setPage(table.getStart());
        page.setRows(tables);
        page.setSize(table.getRows());
        page.setTotal(tableDao.selectCount());
        return page;
    }
}
